<template>
  <div>
     <pre>
           //{{ underlineToCamel($props.tableName) }}.controller.ts


import { Inject, Controller, Get, Query, Post, Body, App, All } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { HttpService } from "@midwayjs/axios";
import { Application } from "../interface.js";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import {  {{ entityName }}  } from "../entity/{{ underlineToCamel($props.tableName) }}.entity.js";
import { BaseDto } from "../models/Base.dto.js";
import { getSkip } from "../utils/pageUtil.js";
import { {{underlineToCamel($props.tableName) }}Service } from "../service/{{underlineToCamel($props.tableName) }}.Service.js";
import { failres, okotherres, okres } from "../config/resUtil.js";


          @Controller('/api/{{ $props.tableName }}')
        export class  {{ underlineToCamel($props.tableName) }}Controller{
              @Inject()
              ctx: Context;
              @App("koa")
              app: Application;

              @Inject()
              {{ underlineToCamel($props.tableName) }}Service: {{ upperFirstConcat(underlineToCamel($props.tableName)) }}Service;

 //find
   @Post("/getlist")
    async getlist(@Body() body: {{ entityName }} & BaseDto) {
      var result3 = await this.{{ underlineToCamel($props.tableName) }}Service.getlist(body);
      return okotherres(result3);
    }
 //findone
    @Get("/getbyid")
    async getbyid(@Query('id') id) {
       var result3 = await this.{{ underlineToCamel($props.tableName) }}Service.getbyid(id);
      return okotherres(result3);
    }
 //add
    @Post("/add")
    async add(@Body() body: {{ entityName }} & BaseDto) {
        var result3 = await this.{{ underlineToCamel($props.tableName) }}Service.add(body);
      return okotherres(result3);
    }
 //update
  @Post("/update")
    async update(@Body() body: {{ entityName }} & BaseDto) {
        var result3 = await this.{{ underlineToCamel($props.tableName) }}Service.update(body);
      return okotherres(result3);
    }
 //delbyid
   @Get("/delbyid")
    async delbyid(@Query('id') id) {
       var result3 = await this.{{ underlineToCamel($props.tableName) }}Service.delbyid(id);
      return okotherres(result3);
    }


        }
      </pre>
  </div>
</template>
<script>

function type2tstype(texttype) {
  if (texttype == 'integer') {
    return 'number';
  } else if (texttype == 'text') {
    return 'string';
  }
}

function upperFirstConcat(str) {
   str = str.toLowerCase();
  str = str.replace(/\b\w+\b/g, word => {
    return word.substring(0, 1).toUpperCase() + word.substring(1);
  });

  return str;
}

function underlineToCamel(str) {
  return str?.replace(/_([a-z])/g, function (match, group1) {
    return group1.toUpperCase();
  });
}

export default {
  data() {
    return {
      upperFirstConcat,
      type2tstype,
      underlineToCamel,
    };
  },

  props: {
    columns: { type: [Array] },
    tableName: { type: [String] },
  },
  computed: {
    entityName() {
      return `${underlineToCamel(this.$props.tableName)}Entity`;
    },
  },
};
</script>
